package com.link.base.base.fund.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.fund.model.DealerAccount;
import com.link.base.base.fund.service.CommonTransactionService;
import com.link.base.base.fund.service.DealerAccountService;
import com.link.base.base.order.model.SaleOrder;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.query.Filter;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 金额交易，账户充值、划款、订单支付模块，记录交易信息
 *
 * @author  姚润发
 * @date:   2016/12
 * @version 1.0
 */

@Controller
@RequestMapping("/link/pay")
public class CommonTransactionController {

    @Resource
    private DealerAccountService dealerAccountService;

    @Resource
    private CommonTransactionService commonTransactionService;

    /**
     * <p>批量支付订单</p>
     *
     * @author yrf
     * @param list
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "batchPayOrder", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> payOrder(@RequestBody List<SaleOrder> list, HttpSession session,
                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            DealerAccount dealerAccount = getCashDealerAccount(session);
            for (int i = 0; i < list.size(); i++) {
                SaleOrder order = list.get(i);
                commonTransactionService.orderPay(order.getId(), dealerAccount.getId(), "", order.getComments());
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>支付订单</p>
     *
     * @author yrf
     * @param orderId
     * @param orderNo
     * @param comments
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "payOrder", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> payOrder(@RequestParam Long orderId, String orderNo, String comments,
                                        HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            DealerAccount dealerAccount = getCashDealerAccount(session);
            commonTransactionService.orderPay(orderId, dealerAccount.getId(), "", comments);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>获取当前客户经销商现金账户</p>
     *
     * @author yrf
     * @param session
     * @return
     * @throws Exception
     */
    private DealerAccount getCashDealerAccount(HttpSession session) throws Exception {
        CoreUser user = UserUtil.getUser();
        if (null == user) {
            throw new ServiceException("PUBLIC-023");
        }
        if (null == user.getAcctId()) {
            throw new ServiceException("PUBLIC-026");
        }
        DealerAccount dealerAccount = new DealerAccount();
        dealerAccount.setPageFlag(false);
//      dealerAccount.setConditionSql(" and accntId='"+user.getAcctId()+"' and type='Cash Account' ");//查询当前用户经销商现金账户
        Filter filter1 = new Filter("accntId", String.valueOf(user.getAcctId()));
        Filter filter2 = new Filter("type", "Cash Account");
        List<Filter> filterList = new ArrayList<Filter>();
        filterList.add(filter1);
        filterList.add(filter2);
        dealerAccount.setFilterList(filterList);
        List<DealerAccount> listAccount = dealerAccountService.queryByExamplePage(dealerAccount);
        if (listAccount == null || listAccount.size() != 1) {
            throw new ServiceException("FUND-020");
        }
        //获取资金账户
        dealerAccount = listAccount.get(0);
        return dealerAccount;
    }

    /**
     * <p>现金账户充值</p>
     *
     * @author yrf
     * @param amount
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "refill", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> refill(@RequestParam double amount, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            DealerAccount dealerAccount = getCashDealerAccount(session);
            //refillCashDealerAccount(dealerAccount, amount);
            commonTransactionService.changeAccount(dealerAccount.getId(), null, amount, null, null, "Change", "余额调整", "");
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
